/*
	Copyright (c) 2007, ANYwebcam.com Pty Ltd. All rights reserved.

	The software in this package is published under the terms of the BSD style 
	license, a copy of which has been included with this distribution in the 
	license.txt file.
*/
package com.anywebcam.di.component
{
	import com.anywebcam.di.*;
	import com.anywebcam.di.component.*;

	import flexunit.framework.TestCase;
	import flexunit.framework.TestSuite;

	public class CachingComponentTest extends TestCase
	{
		public static function suite():TestSuite
		{
			var ts:TestSuite = new TestSuite();

			var tests:Array = 
			[
				'shouldReturnTheSameInstanceForEachCallToGetInstance',
			];

			tests.forEach( function( methodName:String, index:int, array:Array ):void
			{
				ts.addTest( new CachingComponentTest( methodName ) );
			});

			return ts;
		}

		public function CachingComponentTest( method:String )
		{
			super( method );
		}
		
		public function shouldReturnTheSameInstanceForEachCallToGetInstance():void
		{
			var setterComponent:SetterInjectionComponent = new SetterInjectionComponent( ClassWithSetters, ClassWithSetters );
			var component:DIComponent = new CachingComponent( setterComponent );
			
			var instanceOne:ClassWithSetters = component.getInstance( null ) as ClassWithSetters;
			var instanceTwo:ClassWithSetters = component.getInstance( null ) as ClassWithSetters;
			var instanceThree:ClassWithSetters;
			
			assertStrictlyEquals( 'Expecting instanceOne and instanceTwo to be ===', instanceOne, instanceTwo );

			instanceOne.setterProperty = 'testing CachingComponent';
			
			assertEquals( 'Expecting modifications to instanceOne to be reflected in instanceTwo', 
				instanceOne.setterProperty, instanceTwo.setterProperty );
				
			instanceThree = component.getInstance( null ) as ClassWithSetters;
			
			assertEquals( 'Expecting modifications to instanceOne to be reflected in instanceThree after retrieval from CachingComponent', 
				instanceOne.setterProperty, instanceThree.setterProperty );
		}
	}
}